/**   
* @Title: ClaimLaborDao.java 
* @Package com.infodms.dms.dao.claim.basicData 
* @Description: TODO(索赔工时维护DAO) 
* @author Administrator   
* @date 2010-6-1 下午03:02:44 
* @version V1.0   
*/
package com.infodms.dms.dao.claim.basicData;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsWrWrlabinfoPO;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

/** 
 * @ClassName: ClaimLaborDao 
 * @Description: TODO(索赔工时维护DAO) 
 * @author Administrator 
 * @date 2010-6-1 下午03:02:44 
 *  
 */
public class ClaimLaborDao extends BaseDao {
	public static Logger logger = Logger.getLogger(ClaimLaborDao.class);
	private static final ClaimLaborDao dao = new ClaimLaborDao ();
	public static final ClaimLaborDao getInstance() {
		return dao;
	}
	/**
	 * 
	* @Title: getClaimWrModelGroup 
	* @Description: TODO(获取所有的索赔工时车型组列表) 
	* @param @param type    车型组类型
	* @param @return   
	* @return List<TtAsWrModelGroupPO>  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public List getClaimWrModelGroup(Integer type){
		List<Object> params = new LinkedList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append(" select tawmg.wrgroup_id,tawmg.wrgroup_name,tawmg.wrgroup_type from TT_AS_WR_MODEL_GROUP tawmg ");
		sb.append("	where tawmg.wrgroup_type = ?  ");
		params.add(type);
		sb.append("");
		List<Map<String, Object>> list = pageQuery(sb.toString(), params,getFunName());
		return list;
	}
	
	/**
	 * 
	* @Title: claimLaborQuery 
	* @Description: 索赔工时查询
	* @param  pageSize           ：每页显示条数
	* @param  curPage            ：当前页数
	* @param  whereSql           ：查询条件
	* @param  params             ：查询条件对应的参数
	* @param @throws Exception    设定文件 
	* @return PageResult<Map<String,Object>>    返回类型 
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public   PageResult<Map<String, Object>> claimLaborQuery(int pageSize, int curPage,Long oemCompanyId, String whereSql,List<Object> params) throws Exception {
		PageResult<Map<String, Object>> result = null;
		StringBuffer sb = new StringBuffer();
		sb.append(" select g.wrgroup_code,taww.id,taww.wrgroup_id,taww.labour_code,");
		sb.append(" taww.cn_des,trim(to_char(taww.labour_quotiety,'999999.00')) as labour_quotiety,");
		sb.append(" trim(to_char(taww.labour_hour,'999990.00')) as labour_hour ");
		sb.append(" from TT_AS_WR_WRLABINFO taww,tt_as_wr_model_group g where taww.wrgroup_id = g.wrgroup_id and taww.is_del = 0 and taww.oem_company_id="+oemCompanyId+"   ");
		sb.append(" and taww.tree_code = '3' ");
		if(whereSql != null && !"".equals(whereSql.trim())){
			sb.append(whereSql);
		}
		sb.append(" order by taww.id desc ");
		result = (PageResult<Map<String, Object>>) pageQuery(sb.toString(), params, getFunName(), pageSize, curPage);
		return result;
	}
	@SuppressWarnings("unchecked")
	public HashMap getClaimLaborById(Long id){
		HashMap hm = null;
//		List<Object> params = new LinkedList<Object>();
//		StringBuilder sb = new StringBuilder();
//		sb.append(" select taww.id,taww.wrgroup_id,taww.labour_code,taww.cn_des, ");
//		sb.append(" trim(to_char(taww.labour_quotiety,'999999.00')) as labour_quotiety,");
//		sb.append(" trim(to_char(taww.labour_hour,'999999.00')) as labour_hour,tawmg.wrgroup_code from TT_AS_WR_WRLABINFO taww ");
//		sb.append(" left outer join TT_AS_WR_MODEL_GROUP tawmg on taww.wrgroup_id = tawmg.wrgroup_id ");
//		sb.append("	where taww.id = ?  ");
//		params.add(id);
		
		StringBuffer sql= new StringBuffer();
		sql.append("select t.id,\n" );
		sql.append("       t.pater_id,\n" );
		sql.append("       t.wrgroup_id,\n" );
		sql.append("       t.labour_code,\n" );
		sql.append("       t.cn_des,\n" );
		sql.append("       t.labour_quotiety,\n" );
		sql.append("       t.labour_hour,\n" );
		sql.append("       t.wrgroup_code,\n" );
		sql.append("       t2.labour_code as p_labour_code,\n" );
		sql.append("       t2.cn_des as p_cn_des,\n" );
		sql.append("       t2.remark as p_remark\n" );
		sql.append("  from (select taww.id,\n" );
		sql.append("               taww.pater_id,\n" );
		sql.append("               taww.wrgroup_id,\n" );
		sql.append("               taww.labour_code,\n" );
		sql.append("               taww.cn_des,\n" );
		sql.append("               trim(to_char(taww.labour_quotiety, '999999.00')) as labour_quotiety,\n" );
		sql.append("               trim(to_char(taww.labour_hour, '999999.00')) as labour_hour,\n" );
		sql.append("               tawmg.wrgroup_code\n" );
		sql.append("          from TT_AS_WR_WRLABINFO taww\n" );
		sql.append("          left outer join TT_AS_WR_MODEL_GROUP tawmg on taww.wrgroup_id =\n" );
		sql.append("                                                        tawmg.wrgroup_id) t,\n" );
		sql.append("       TT_AS_WR_WRLABINFO t2\n" );
		sql.append(" where t.pater_id = t2.id\n" );
		sql.append("   and t.id = "+id+" ");

		
		List<Map<String, Object>> list = pageQuery(sql.toString(), null, getFunName());
		if(list != null && list.size() > 0){
			hm = (HashMap)list.get(0);
		}
		return hm;
	}
	/**
	 * 
	* @Title: getAdddClaimLaborById 
	* @Description: TODO(根据ID查询附加工时信息) 
	* @param @param id
	* @param @return   
	* @return HashMap  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public HashMap getAdddClaimLaborById(Long id){
		HashMap hm = null;
		StringBuffer sql= new StringBuffer();
		sql.append("select ts.id,\n" );
		sql.append("       ts.pater_id,\n" );
		sql.append("       ts.wrgroup_id,\n" );
		sql.append("       ts.labour_code,\n" );
		sql.append("       ts.cn_des,\n" );
		sql.append("       trim(to_char(ts.labour_quotiety, '999999.00')) as labour_quotiety,\n" );
		sql.append("       trim(to_char(ts.labour_hour, '999999.00')) as labour_hour\n" );
		sql.append("        from TT_AS_WR_WRLABINFO ts\n" );
		sql.append("       where ts.id = "+id+" ");
		List<Map<String, Object>> list = pageQuery(sql.toString(), null, getFunName());
		if(list != null && list.size() > 0){
			hm = (HashMap)list.get(0);
		}
		return hm;
	}	
	/**
	 * 
	* @Title: getBussCodeById 
	* @Description: TODO(根据索赔工时id获取故障代码列表) 
	* @param @param id
	* @param @return   
	* @return List  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public List getBussCodeById(Long id){
		List<Object> params = new LinkedList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append(" select tbcc.code,tbcc.code_name,tbcc.business_code_id,tawtm.map_id ");
		sb.append(" from TT_AS_WR_TROBLE_MAP tawtm ");
		sb.append(" left outer join TM_BUSINESS_CHNG_CODE tbcc  ");
		sb.append(" on tawtm.trouble_id = tbcc.business_code_id ");
		sb.append("	where tawtm.is_del = 0 and tawtm.labor_id = ?  ");
		params.add(id);
		List<Map<String, Object>> list = pageQuery(sb.toString(), params, getFunName());
		return list;
	}
	
	/**
	 * 
	* @Title: getAddLabourById 
	* @Description: TODO(根据索赔工时id获取附加工时列表) 
	* @param @param id
	* @param @return   
	* @return List<TtAsWrWrlabinfoPO>  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public List getAddLabourById(Long id){
		List<Object> params = new LinkedList<Object>();
		StringBuilder sb = new StringBuilder();
//		sb.append(" select * from TT_AS_WR_WRLABINFO  taww ");
//		sb.append(" where exists ");
//		sb.append(" (select 'X' from TT_AS_WR_ADDITIONALITEM  tawa ");
//		sb.append(" where taww.id=tawa.add_id ");
//		sb.append(" and taww.is_del = 0 ");
//		sb.append(" and tawa.is_del = 0 ");
//		sb.append("	and tawa.w_id = ? )");
		sb.append("select tawa.id,taww.wrgroup_id,taww.labour_code,taww.cn_des,taww.id as add_id,");
		sb.append(" trim(to_char(taww.labour_quotiety,'999999.00')) as labour_quotiety, ");
		sb.append(" trim(to_char(taww.labour_hour,'999999.00')) as labour_hour ");
		sb.append(" from TT_AS_WR_ADDITIONALITEM tawa ");
		sb.append(" left outer join TT_AS_WR_WRLABINFO  taww on tawa.add_id = taww.id ");
		sb.append(" where tawa.is_del = 0 and taww.is_del = 0 and tawa.w_id = ? ");
		sb.append("");
		params.add(id);
		List<Map<String, Object>> list = pageQuery(sb.toString(), params, getFunName());
		return list;
	}
	/**
	 * 
	* @Title: getBusinessCodeByType 
	* @Description: TODO(取得所有故障代码的列表) 
	* @param @param type ：10441004
	* @param @return   
	* @return List  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public List getBusinessCodeByType(Integer type,String laborId,Long companyId){
		List<Object> params = new LinkedList<Object>();
		StringBuilder sb = new StringBuilder();
		sb.append("select tbcc.business_code_id,tbcc.code,tbcc.code_name ");
		sb.append(" from TM_BUSINESS_CHNG_CODE tbcc  where tbcc.is_del=0 and  tbcc.oem_company_id ="+companyId+" and tbcc.type_code = ? ");
		params.add(type);
		sb.append(" and  not exists (select 'X' from TT_AS_WR_TROBLE_MAP tawtm ");
		sb.append(" where tbcc.business_code_id=tawtm.trouble_id and tawtm.labor_id = ? ");
		sb.append(" ) order by tbcc.code ");
		params.add(laborId);
		List<Map<String, Object>> list = pageQuery(sb.toString(), params, getFunName());
		return list;
	}
	/**
	 * 
	* @Title: laborUpdate 
	* @Description: TODO(索赔工时/附加工时修改) 
	* @param @param selpo
	* @param @param updatepo   
	* @return void  
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void laborUpdate(PO selpo,PO updatepo){
		dao.update(selpo, updatepo);
	}
	
	@SuppressWarnings("unchecked")
	public void claimlaborUpdate(TtAsWrWrlabinfoPO selpo,TtAsWrWrlabinfoPO updatepo){
		dao.update(selpo, updatepo);
	}
	
	@SuppressWarnings("unchecked")
	public void deleteClaimlabor(Long id,TtAsWrWrlabinfoPO selpo,TtAsWrWrlabinfoPO delpo){
		List<Object> params = new LinkedList<Object>();
		StringBuilder sb = null;
		//删除该索赔工时对应附加工时信息
		sb = new StringBuilder();
		sb = new StringBuilder(" delete from  TT_AS_WR_ADDITIONALITEM where w_id = ? ");
		params.add(id);
		dao.delete(sb.toString(), params);
		params.clear();
		//删除该索赔工时对应故障
		sb = new StringBuilder(" delete from  TT_AS_WR_TROBLE_MAP where trouble_id = ? ");
		params.add(id);
		dao.delete(sb.toString(), params);
		//删除该索赔工时
		dao.update(selpo, delpo);

		
	}

	/* (非 Javadoc) 
	 * <p>Title: wrapperPO</p> 
	 * <p>Description: </p> 
	 * @param rs
	 * @param idx
	 * @return 
	 * @see com.infodms.dms.dao.common.BaseDao#wrapperPO(java.sql.ResultSet, int) 
	 */
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO 自动生成方法存根
		return null;
	}

}
